package com.gemme.gemme.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.MainActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.home.PageAllProductBean;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.DpUtil;
import com.gemme.gemme.util.SPUtil;
import com.gemme.gemme.view.Interest2View;
import com.gemme.gemme.view.InterestView;
import com.gemme.gemme.view.MyButton;
import com.gemme.gemme.view.OnChangeLIstener;
import com.jaeger.library.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//侧滑删除地址
public class Interset2Activity extends BaseActivity {

    boolean isNext = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest2);
        StatusBarUtil.setTransparent(this);

        judge();
        //设置为首次登录 todo
//        SPUtil.PutData(mContext, Constant.isFirstLogin, "0");

        initView();
        initData();

        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        Log.d("Interset2Activity", "width = " + DpUtil.px2dip(this,width) + ",height = " +  DpUtil.px2dip(this,height));
        Log.d("Interset2Activity", "width = " + width + ",height = " +  height);

    }
    private void judge() {
        //todo 有登陆信息直接进入主页
        String ACCESS_TOKEN = SPUtil.getDataForString(mContext, Constant.ACCESS_TOKEN, "");

        if(!TextUtils.isEmpty(ACCESS_TOKEN)){
//            startActivity(new Intent(this, PersonInfoActivity.class));
            startActivity(new Intent(this, MainActivity.class));
            finish();
            return;
        }

        //设置为首次登录 todo
//        SPUtil.PutData(mContext, Constant.isFirstLogin, "0");
     /*   String dataForString = SPUtil.getDataForString(mContext, Constant.isFirstLogin, "0");
        if("0".equals(dataForString)){
            SPUtil.PutData(mContext,Constant.isFirstLogin,"-1");
            SPUtil.getDataForString(mContext, Constant.isFirstLogin, "0");
        }else{
            startActivity(new Intent(this,LoginNewActivity.class));
            finish();
        }*/


       String interest = SPUtil.getDataForString(mContext,Constant.INTEREST,"");
       String sex = SPUtil.getDataForString(mContext,Constant.user_sex,"");
       String user_birthDay = SPUtil.getDataForString(mContext,Constant.user_birthDay,"");

       if(TextUtils.isEmpty(interest)){//兴趣为空,兴趣页
           return;
       }

       if(TextUtils.isEmpty(sex) && TextUtils.isEmpty(user_birthDay)){//性别和出生日期都为空
           startActivity(new Intent(mContext,SexAgeActivity.class));
           finish();
           return;
       }


       //兴趣和性别和出生日期都有值,未登录,进入登录页
        startActivity(new Intent(this,MainActivity.class));
       finish();
    }



    private void initData() {
        String url = DataManager.BaseUrl_weicheng + "user/userapp/getInterestClassification" ;

//        HashMap<String, String> headMap = new HashMap<>();
//        headMap.put(DataManager.AuthorizationKey,"Bearer 2af9350c-dad4-4e04-b5b9-6a931f9b7b0c");
        DataManager.toServer3(this, url, DataManager.GET_TYPE, "", null, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    List<InterestBean> interestBeans = JSON.parseArray(response, InterestBean.class);
                    System.out.println("");
                    setData(interestBeans);
                }catch (Exception e){

                }
            }
        });
    }
/*
* item1
item2
item3
item4
* */
    private void setData(List<InterestBean> interestBeans) {
        try {
            for (int i = 0; i < interestBeans.size(); i++) {
                InterestBean interestBean = interestBeans.get(i);
                String name = interestBean.getName();
                if("护肤".equals(name)){
                    item1.setText(name).setPicture(interestBean.getImage()) ;
                }else if("彩妆".equals(name)){
                    item2.setText(name).setPicture(interestBean.getImage()) ;
                }else if("男士".equals(name)){
                    item3.setText(name).setPicture(interestBean.getImage()) ;
                }else if("香氛".equals(name)){
                   item4.setText(name).setPicture(interestBean.getImage()) ;
                }
            }
        }catch (Exception e){

        }
    }



    Interest2View item1;
    Interest2View item2;
    Interest2View item3;
    Interest2View item4;
    TextView tv_next;
    TextView tv_skip;
    private void initView() {
        item1 = findViewById(R.id.item1);
        item2 = findViewById(R.id.item2);
        item3 = findViewById(R.id.item3);
        item4 = findViewById(R.id.item4);


        tv_next = findViewById(R.id.tv_next);
        tv_skip = findViewById(R.id.tv_skip);
        changeNext();

        item1.setOnChangeLIstener(onChangeLIstener);
        item2.setOnChangeLIstener(onChangeLIstener);
        item3.setOnChangeLIstener(onChangeLIstener);
        item4.setOnChangeLIstener(onChangeLIstener);

        tv_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SPUtil.PutData(mContext,Constant.INTEREST,interestStr);
                String dataForString = SPUtil.getDataForString(mContext, Constant.INTEREST,"");
                System.out.println();
                startActivity(new Intent(mContext,SexAgeActivity.class));
                finish();

            }
        });
        tv_skip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳过,设置标记
                    SPUtil.PutData(mContext,Constant.INTEREST,Constant.type_qiyong);

                    startActivity(new Intent(mContext,SexAgeActivity.class));
                    finish();
                }
            });

    }

    OnChangeLIstener onChangeLIstener= new OnChangeLIstener(){
        @Override
        public void onChangeLIstener() {
            getState();
        }
    };
    String interestStr = "";
    private void getState() {


        if(item1.getState() || item2.getState() || item3.getState() || item4.getState()){
            isNext = true;
        }else{
            isNext = false;
        }
        interestStr = (item1.getState() ? item1.getName() + "," : "") +
                (item2.getState() ? item2.getName() + "," : "") +
                (item3.getState() ? item3.getName() + "," : "") +
                (item4.getState() ? item4.getName() + ","  : "")
        ;


        changeNext();
    }

    public void changeNext(){
        if(isNext){
            tv_next.setVisibility(View.VISIBLE);
        }else{
            tv_next.setVisibility(View.GONE);
        }
    }



}